﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.Core;

namespace Cesave.Domain.PermisosModule.Contracts.Concesiones
{   
        public class ConcesionService
          : IConcesionService
        {
            private IConcesionRepository _concesionrepository;

            public ConcesionService(IConcesionRepository concesionrepository)
            {
                _concesionrepository = concesionrepository;
            }

            public void AddConcesion(Entities.Concesion concesion)
            {
                IUnitOfWork unitofwork = _concesionrepository.StoreContext;
                _concesionrepository.Add(concesion);
                try
                {
                    unitofwork.Commit();
                }
                catch (Exception)
                {
                    unitofwork.RollbackChanges();
                    unitofwork.CommitAndRefreshChanges();
                }
            }

            public void ChangeConcesion(Entities.Concesion concesion)
            {
                IUnitOfWork unitofwork = _concesionrepository.StoreContext;
                _concesionrepository.Modify(concesion);
                try
                {
                    unitofwork.Commit();
                }
                catch (Exception)
                {
                    unitofwork.RollbackChanges();
                    unitofwork.CommitAndRefreshChanges();
                }
            }

            public Entities.Concesion FindConcesionByGuid(Guid concesionGuid)
            {
                ConcesionByGuidSpecification specification = new ConcesionByGuidSpecification(concesionGuid);
                return _concesionrepository.FindConcesionByGuidSpecification(specification);
            }

            public void Dispose()
            {
                if (_concesionrepository != null && _concesionrepository.StoreContext != null)
                    _concesionrepository.StoreContext.Dispose();
            }
        }
}
